﻿using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using System.Xml.Linq;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using WMS.Seven.Models;

namespace WMSProject.XiangHe.Procurements
{
    [ApiExplorerSettings(GroupName = "项鹤")]
    public class ProcurementService : ApplicationService, IProcurementService
    {
        /// <summary>
        /// 采购
        /// </summary>
        private readonly IRepository<Procurement, int> _prorepository;
        /// <summary>
        /// 采购明细
        /// </summary>
        private readonly IRepository<PurchaseDetails, int> _purrepository;
        /// <summary>
        /// 产品
        /// </summary>
        private readonly IRepository<Products, int> _durepository;
        public ProcurementService(IRepository<Procurement, int> prorepository, IRepository<Products, int> durepository, IRepository<PurchaseDetails, int> purrepository)
        {
            _prorepository = prorepository;
            _durepository = durepository;
            _purrepository = purrepository;
        }

        /// <summary>
        /// 获取采购列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet,Route("/XiangHe/GetProcument")]
        public async Task<ReturnInfo<List<ProcurementDTO>>> GetProcument(int pageIndex, int pageSize, string? curCode, string? proCode, string? proName)
        {
            var proList = from a in await _prorepository.GetListAsync()
                          join b in await _purrepository.GetListAsync()
                          on a.ProcurementCode equals b.PurchaseOrderCode
                          join c in await _durepository.GetListAsync()
                          on b.ProductCode equals c.ProductCode
                          select new ProcurementDTO
                          {
                              ProcurementId = a.Id,
                              ProcurementCode = b.PurchaseOrderCode,
                              ProcurementDate = a.ProcurementDate,
                              ProductName = c.ProductName,
                              ProductCode = c.ProductCode,
                              Unit = c.Unit,
                              UnitPrice = c.UnitPrice,
                              Quantity = b.Quantity,
                              SupplierCode = a.SupplierCode,
                              SupplierName = a.SupplierName,
                              ProductSpecification = c.ProductSpecification,
                              ProStatus=a.ProStatus,
                              IsRu=a.IsRu,
                              IsReturn=a.IsReturn
                          };
            if (!string.IsNullOrEmpty(curCode))
            {
                proList = proList.Where(a => a.ProcurementCode.Contains(curCode));
            }
            if (!string.IsNullOrEmpty(proCode))
            {
                proList = proList.Where(a => a.ProductCode.Contains(proCode));
            }
            if (!string.IsNullOrEmpty(proName))
            {
                proList = proList.Where(a => a.ProductName.Contains(proName));
            }
            var totalCount = proList.Count();
            proList = proList.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            var list = AutoMapperExt.MapToList<ProcurementDTO>(proList);
            if (proList != null)
            {
                return new ReturnInfo<List<ProcurementDTO>>(true, "0", list, totalCount);
            }
            return new ReturnInfo<List<ProcurementDTO>>(true, "1", null, 0);
        }

        /// <summary>
        /// 采购添加
        /// </summary>
        /// <param name="prodto"></param>
        /// <returns></returns>
        [HttpGet,Route("/XiangHe/AddProcure")]
        public async Task<int> AddProcure(ProcurementDTO prodto)
        {
            try
            {
                using TransactionScope scope = new(TransactionScopeOption.Required, new TransactionOptions
                { IsolationLevel=IsolationLevel.Serializable});
                var pro = new Procurement()
                {
                    ProcurementCode=prodto.ProcurementCode,
                    DocumentType=prodto.DocumentType,
                    SupplierCode=prodto.SupplierCode,
                    SupplierName=prodto.SupplierName,
                    ProcurementDate = prodto.ProcurementDate,
                    CreateDate=DateTime.Now,
                    SaleCode=prodto.SaleCode,
                    ReceivingDate=prodto.ReceivingDate,
                    ClientName= prodto.ClientName,
                    Contact=prodto.Contact,
                    Phone=prodto.Phone,
                    Buyer=prodto.Buyer,

                };
                
                return 1;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public Task<ReturnInfo<List<Procurement>>> GetProcument(int pageIndex, int pageSize)
        {
            throw new NotImplementedException();
        }
    }
}